const {calendar} = require('/calendar.js');
/**
 * 填充农历日期
 * @param solarDateRange
 * @returns {Array}
 */
const initLunarDateRangeHandler = function (solarDateRange) {
    console.log("公历日期范围：", solarDateRange);
    const lunarDateRange = [];
    const lunarYearRange = [], lunarMonthRange = [], lunarDayRange = [];
    for (let i = solarDateRange.start; i <= solarDateRange.end; i++) {
        lunarYearRange.push({text: calendar.toGanZhiYear(i) + '(' + i + ')', value: i});
    }
    for (let i = 1; i <= 12; i++) {
        lunarMonthRange.push({text: calendar.toChinaMonth(i), value: i, isLeapMonth: false});
    }
    for (let i = 1; i <= 29; i++) {
        lunarDayRange.push({text: calendar.toChinaDay(i), value: i});
    }
    lunarDateRange.push(lunarYearRange);
    lunarDateRange.push(lunarMonthRange);
    lunarDateRange.push(lunarDayRange);
    console.log("农历日期：", lunarDateRange);
    return lunarDateRange;
};

/**
 * 重置农历月份
 * @param selectYear
 * @returns {Array}
 */
const resetLunarMonthRangeHandler = function (selectYear) {
    const leapMonth = calendar.leapMonth(selectYear);
    const lunarMonthRange = [];
    console.log("选择年份：", selectYear, "闰月：", leapMonth);
    for (let i = 1; i <= 12; i++) {
        lunarMonthRange.push({text: calendar.toChinaMonth(i), value: i, isLeapMonth: false});
    }
    if (leapMonth > 0) {
        console.log("有闰月，准备更新月份数据");
        lunarMonthRange.splice(leapMonth, 0, {
            text: "闰" + calendar.toChinaMonth(leapMonth),
            value: leapMonth,
            isLeapMonth: true
        });
    }
    return lunarMonthRange;
};

/**
 * 重置农历日
 * @param selectYear
 * @param selectMonth
 * @param isLeapMonth
 * @returns {Array}
 */
const resetLunarDayRangeHandler = function (selectYear, selectMonth, isLeapMonth) {
    const lunarDayRange = [];
    console.log("选择年份：", selectYear, "选择月份：", selectMonth, "是否闰月：", isLeapMonth);
    const days = isLeapMonth ? calendar.leapDays(selectYear) : calendar.monthDays(selectYear, selectMonth);
    for (let i = 1; i <= days; i++) {
        lunarDayRange.push({text: calendar.toChinaDay(i), value: i});
    }
    return lunarDayRange;
};

module.exports = {initLunarDateRangeHandler, resetLunarMonthRangeHandler, resetLunarDayRangeHandler};